package org.example.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.Sell;
import org.example.domain.TakePosition;
import org.example.domain.Three;
import org.example.service.SellService;
import org.example.service.TakePositionService;
import org.example.service.ThreeService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("sell")
@Slf4j
public class SellController {
    @Autowired
    private SellService sellService;
    @Autowired
    private ThreeService threeService;
    @Autowired
    private TakePositionService takePositionService;
    //我的委托
    @RequestMapping("sellList")
    public Result sell(String token, @RequestBody Sell sell) {
        JWT jwt = JWTUtil.parseToken(token);
        Integer id = (Integer) jwt.getPayload("id");
        sell.setUserId(id);
        List<Sell> list = sellService.getSellById(sell);
        return Result.success(list);
    }
    @RequestMapping("delSell")
    public Result delSell(Integer id) {
        Sell byId = sellService.getById(id);
        byId.setIssell(1);
        boolean b = sellService.updateById(byId);
        return b?Result.success():Result.fail();
    }
    //所有委托
    @RequestMapping("allSellList")
    public Result allSellList(HttpServletRequest request,Integer stockId) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("id");
        List<Sell> list = sellService.alllist(userId,stockId);
        return Result.success(list);
    }
    @RequestMapping("getThreeList")
    public Result getThreeList(String code,HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("id");
        List<Three> list=threeService.getThreeList(code,userId);
        return Result.success(list);
    }
    @RequestMapping("successForHe")
    public Result successForHe(HttpServletRequest request,@RequestBody Three three){
        TakePosition takePosition = new TakePosition();
        takePosition.setStockId(three.getStockId());
        takePosition.setUserId(three.getBuyId());
        takePosition.setVolumeCode(IdUtil.getSnowflake().nextIdStr());
        takePosition.setNum(three.getBuyNum());
        takePosition.setVolumePrice(three.getSellPrice());
        takePosition.setCreateDate(new Date());
        takePosition.setAveragePrice(three.getAveragePrice());
        String buy = takePositionService.buyorThree(takePosition, three.getSellId(),three.getVolumeCode());
        return buy.equals("购买成功")?Result.success("交易成功"):Result.fail(buy);
    }
}
